package com.rattsoftware.client.controllers;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.rattsoftware.client.RefreshableEvent;
import com.rattsoftware.client.RefreshablePublisher;
import com.rattsoftware.client.webclient;
import com.rattsoftware.client.events.AppEvents;
import com.rattsoftware.client.model.StaffSummary;
import com.rattsoftware.client.model.TestData;
import com.rattsoftware.client.util.ProgressIndicator;
import com.rattsoftware.client.views.StaffView;
import com.rattsoftware.server.admin.model.Staff;
import com.rattsoftware.server.admin.model.User;
import com.rattsoftware.server.admin.model.exceptions.RattsParsingException;

public class StaffController extends Controller {

	private StaffView staffView;
	
	public StaffController() {
		registerEventTypes(AppEvents.Init);
		registerEventTypes(AppEvents.StaffView);
	}
	
	@Override
	public void handleEvent(AppEvent event) {
		GWT.log("StaffController : handleEvent");
		EventType type = (EventType) event.getType();
		if (type == AppEvents.Init) {
			forwardToView(staffView, event);
		} else if (type == AppEvents.StaffView) {
			forwardToView(staffView, event);
		}
	}

	public void initialize() {
		super.initialize();
		GWT.log("StaffController : initialize");
		staffView = new StaffView(this);
		
		RefreshablePublisher.get().subscribe(RefreshableEvent.Type.CREATE_STAFF, staffView);
		RefreshablePublisher.get().subscribe(RefreshableEvent.Type.UPDATE_STAFF, staffView);
		RefreshablePublisher.get().subscribe(RefreshableEvent.Type.DELETE_STAFF, staffView);
	}
	
	public void getStaffList(final PagingLoadConfig pagingLoadConfig, 
			final AsyncCallback<com.extjs.gxt.ui.client.data.PagingLoadResult<StaffSummary>> callback)
			throws RattsParsingException {
		GWT.log("StaffController : getStaffList");
		User user = Registry.get(webclient.LOGGED_IN_USER_NAME);
		
		/*courseService.getCourseList(user, PagingUtil.createPagingLoadConfig(pagingLoadConfig),
				new RattsAsyncCallback<PagingLoadResult<Course>>() {
					@Override
					public void onSuccess(PagingLoadResult<Course> result) {*/
						ProgressIndicator.hideProgressBar();
						List<StaffSummary> results = new ArrayList<StaffSummary>();
						//List<Course> data = result.getData();
						List<Staff> data = TestData.getStaff();
						
						for (Staff f : data) {
							results.add(new StaffSummary(f));
						}
						staffView.setAllStaffSummaries(results); // reset the list
						callback.onSuccess(new BasePagingLoadResult<StaffSummary>(results, 0, 20));
						/*callback.onSuccess(new BasePagingLoadResult<CourseSummary>(results, result.getOffset(), result
								.getTotalLength()));
					}
				});*/
	}
	
	public void forwardToNewStaffController() {
		GWT.log("StaffController : forwardToNewStaffController");
		Dispatcher dispatcher = Dispatcher.get();
		dispatcher.dispatch(AppEvents.NewStaff);
	}
	
	public void forwardToEditStaffController(StaffSummary staffSummary) {
		GWT.log("StaffController : forwardToEditStaffController");
		Dispatcher dispatcher = Dispatcher.get();
		dispatcher.dispatch(AppEvents.EditStaff, staffSummary);
	}
}
